package com.hq.fxh.No2_BBS.Fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.No2_BBS.Activity.Plate_DetialActivity;
import com.hq.fxh.No2_BBS.Activity.PlateSelectActivity;
import com.hq.fxh.No2_BBS.Adapter.Ladapter_PlateName;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.Modle.PlateSoreModle;
import com.hq.fxh.R;
import com.hq.fxh.Utils.ClickUtils;
import com.hq.fxh.Utils.LogUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/18
 * @Email xlwenhao1858@sina.com
 */


public class Plate_Name_Fragment extends BaseFragment {
    protected View rootView;
    protected ListView listPlatName;
    List<PlateSoreModle.DataBean.ItemsBean> plateData;
    int type = 1, firstID;

    public void setType(int type) {
        this.type = type;
    }

    public void setData(List<PlateSoreModle.DataBean.ItemsBean> data, int firstID) {
        this.plateData = data;
        this.firstID = firstID;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_plate_name;
    }

    @Override
    protected void initData() {
        Ladapter_PlateName adapter = new Ladapter_PlateName(plateData, new Ladapter_PlateName.PlateNameCallBack() {
            @Override
            public void onCallFocus(View view, int position, Object data) {
                LogUtils.e("关注" + position);
                PlateSoreModle.DataBean.ItemsBean itemsBean = plateData.get(position);

                if (itemsBean.getState() == 1) {
                    ClickUtils.clickGuanzhu(getContext(), (TextView) view, false);
                    itemsBean.setState(0);
                    followPlateSort(2, itemsBean.getId());
                } else {
                    ClickUtils.clickGuanzhu(getContext(), (TextView) view, true);
                    itemsBean.setState(1);
                    followPlateSort(1, itemsBean.getId());
                }
            }

            @Override
            public void onCallName(View view, int position, Object data) {
                PlateSoreModle.DataBean.ItemsBean dataBean = (PlateSoreModle.DataBean.ItemsBean) data;
                if (type == 2) {
                    PlateSelectActivity activity = (PlateSelectActivity) getActivity();
                    activity.setSecondSort(dataBean.getId(), dataBean.getCategory());
                    activity.setPlateSeclect(data.toString());
                } else {
                    Intent intent = new Intent(getContext(), Plate_DetialActivity.class);
                    intent.putExtra("firstID", firstID);
                    intent.putExtra("secondID", dataBean.getId());
                    startActivity(intent);
                }
            }
        });
        listPlatName.setAdapter(adapter);


        listPlatName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LogUtils.e("itemClick");
            }
        });
    }

    @Override
    protected void findView(View rootView) {
        listPlatName = (ListView) rootView.findViewById(R.id.list_Plat_Name);
    }


    public void followPlateSort(int type, int id) {
        UserServiceImpl.followerPlater(type, firstID, id, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body != null) {

                    try {
                        String string = body.string();
                        LogUtils.startToash(getContext(), "关注成功");
                    } catch (IOException e) {
                        LogUtils.startToash(getContext(), "关注失败" + "error");
                    }

                } else {
                    try {
                        ResponseBody responseBody = response.errorBody();
                        String string = responseBody.string();
                        if (string == null) {
                            LogUtils.startToash(getContext(), "关注失败");
                        } else {
                            LogUtils.startToash(getContext(), "关注失败" + string);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.startToash(getContext(), "关注失败" + t.getMessage());
            }
        });
    }
}
